<?php   if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class employee_model extends CI_Model{

    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
    }
    
    function load_datatable_employee($GET,$step = 1){
        
        $aColumns = array( 'id', 'code', 'name', 'cmnd_number', 'cmnd_where', 'birthday', 'phone', 'address'  );
        
        $this->db->select('*');
        $this->db->from('sy_employee');
        $this->db->where('status !=',-1);
    
        /*
         * Ordering
         */
        if ( isset( $GET['iSortCol_0'] ) )
        {
            for ( $i=0 ; $i<intval( $GET['iSortingCols'] ) ; $i++ )
            {
                if ( $GET[ 'bSortable_'.intval($GET['iSortCol_'.$i]) ] == "true" )
                {
                    if( $aColumns[ $GET['iSortCol_'.$i] ] != ''){
                        $this->db->order_by($aColumns[ $GET['iSortCol_'.$i] ], $GET['sSortDir_'.$i] );
                    }
                }
            }
        }

        /* 
         * Filtering
         */
        if ( isset($GET['sSearch']) && $GET['sSearch'] != "" )
        {   
            if($aColumns[1] != ''){
                $this->db->like($aColumns[1], $GET['sSearch'] );    
                $this->db->or_like($aColumns[2], $GET['sSearch'] );
                $this->db->or_like($aColumns[3], $GET['sSearch'] );
            }
        }
    
        /* Individual column filtering */
        for ( $i=0 ; $i<count($aColumns) ; $i++ )
        {
            if ( isset($GET['bSearchable_'.$i]) && $GET['bSearchable_'.$i] == "true" && $GET['sSearch_'.$i] != '' )
            {
                if($aColumns[$i] != ''){
                    $this->db->like($aColumns[$i], $GET['sSearch_'.$i] );
                }
            }
        }
        
        $total = 0;
        $aaData = array();
        
        switch($step){
            case 1:
                /* 
                 * Paging
                 */
                if ( isset( $GET['iDisplayStart'] ) && $GET['iDisplayLength'] != '-1' ){
                    $this->db->limit($GET['iDisplayLength'], $GET['iDisplayStart']);
                }
                
                $query = $this->db->get();
                if($query->num_rows() > 0){
                    $total = $query->num_rows();
                }
                
                if($total > 0){
                    foreach($query->result() As $item){
                        $row = array();
                        $row[] = $item->id;
                        $row[] = $item->code;
                        $row[] = $item->name;
                        $row[] = $item->cmnd_number;
                        $row[] = $item->cmnd_where;
                        $row[] = dateMysqlToVN($item->birthday);
                        $row[] = $item->phone;
                        $row[] = $item->address;
                        $row['id'] = $item->id;
                        $row['modify'] = 'edit';
                        $aaData[] = $row;
                    }
                }    
            
            break;
            
            case 2:
                $query = $this->db->get();
                if($query->num_rows() > 0){
                    $total = $query->num_rows();
                }
            break;
            
            default:
            
            break;
        }
        
        $output = array(
            "total" => $total,
            "aaData" => $aaData
        );
    
        return $output;
    }
    
    function getEmployeeById($id){
        $query = $this->db->get_where('sy_employee', array('id' => $id));
        if($query->num_rows() == 1) return $query->row();
        return false;
    }
    
    function unique_employee($code){
        $query = $this->db->get_where('sy_employee', array('code' => $code));
        if($query->num_rows() > 0){
            return false;
        }
        return true;
    }    
    function unique_employee_edit($id,$code){
        $this->db->select('code');
        $this->db->from('sy_employee');
        $this->db->where('id !=', $id);
        $this->db->where('code', $code);
        $query = $this->db->get();
        
        if($query->num_rows() > 0){
            return false;
        }
        return true;
    }
    
    function insertEmployee($data){
        $this->db->insert('sy_employee', $data);
        return $this->db->insert_id();
    }
    function updateEmployee($data, $id){
        $this->db->where('id', $id);
        $this->db->update('sy_employee', $data); 
    }
    
    function load_datatable_permission($GET,$step = 1){
        
        $aColumns = array( 'permission_id', 'name', 'description', 'status' );
        
        $this->db->select('*');
        $this->db->from('sy_permissions');
        $this->db->where('status !=',-1);
    
        /*
         * Ordering
         */
        if ( isset( $GET['iSortCol_0'] ) )
        {
            for ( $i=0 ; $i<intval( $GET['iSortingCols'] ) ; $i++ )
            {
                if ( $GET[ 'bSortable_'.intval($GET['iSortCol_'.$i]) ] == "true" )
                {
                    if( $aColumns[ $GET['iSortCol_'.$i] ] != ''){
                        $this->db->order_by($aColumns[ $GET['iSortCol_'.$i] ], $GET['sSortDir_'.$i] );
                    }
                }
            }
        }

        /* 
         * Filtering
         */
        if ( isset($GET['sSearch']) && $GET['sSearch'] != "" )
        {   
            if($aColumns[1] != ''){
                $this->db->like($aColumns[1], $GET['sSearch'] );    
                //$this->db->or_like($aColumns[0], $GET['sSearch'] );
            }
        }
    
        /* Individual column filtering */
        for ( $i=0 ; $i<count($aColumns) ; $i++ )
        {
            if ( isset($GET['bSearchable_'.$i]) && $GET['bSearchable_'.$i] == "true" && $GET['sSearch_'.$i] != '' )
            {
                if($aColumns[$i] != ''){
                    $this->db->like($aColumns[$i], $GET['sSearch_'.$i] );
                }
            }
        }
        
        $total = 0;
        $aaData = array();
        
        switch($step){
            case 1:
                /* 
                 * Paging
                 */
                if ( isset( $GET['iDisplayStart'] ) && $GET['iDisplayLength'] != '-1' ){
                    $this->db->limit($GET['iDisplayLength'], $GET['iDisplayStart']);
                }
                
                $query = $this->db->get();
                if($query->num_rows() > 0){
                    $total = $query->num_rows();
                }
                
                if($total > 0){
                    foreach($query->result() As $item){
                        $html = '';
                        if($item->status == -1){
                            $html = '<img src="'.base_url('template/ess/images/trash.png').'" alt="">';
                        }elseif($item->status == 1){
                            $html = '<img src="'.base_url('template/ess/images/tick.png').'" alt="">';
                        }elseif($item->status == 2){
                            $html = '<img src="'.base_url('template/ess/images/publish_x.png').'" alt="">';
                        }
                        
                        $row = array();
                        //$row[] = '<input type="radio" name="tableselected" value="">';
                        $row[] = $item->permission_id;
                        $row[] = $item->name;
                        $row[] = $item->description;
                        $row[] = $html;
                        $row['id'] = $item->permission_id;
                        $row['modify'] = 'edit';
                        $aaData[] = $row;
                    }
                }    
            
            break;
            
            case 2:
                $query = $this->db->get();
                if($query->num_rows() > 0){
                    $total = $query->num_rows();
                }
            break;
            
            default:
            
            break;
        }
        
        $output = array(
            "total" => $total,
            "aaData" => $aaData
        );
    
        return $output;
    }
    
    function getRoleActive(){
        $query = $this->db->get_where('sy_roles', array('deleted' => 0));
        if($query->num_rows() > 0) return $query->result();
        return false;
    }
    
    function getRoleById($roleId){
        $query = $this->db->get_where('sy_roles', array('role_id' => $roleId));
        if($query->num_rows() == 1) return $query->row_array();
        return false;
    }
    function unique_role($name){
        $query = $this->db->get_where('sy_roles', array('role_name' => $name));
        if($query->num_rows() > 0){
            return false;
        }
        return true;
    }
    function unique_role_edit($id,$name){
        $this->db->select('role_name');
        $this->db->from('sy_roles');
        $this->db->where('role_id !=', $id);
        $this->db->where('role_name', $name);
        $query = $this->db->get();
        
        if($query->num_rows() > 0){
            return false;
        }
        return true;
    }
    function insertRole($data){
        $this->db->insert('sy_roles', $data);
        return $this->db->insert_id();
    }
    function updateRole($data, $id){
        $this->db->where('role_id', $id);
        $this->db->update('sy_roles', $data); 
    }
    
    function getAllPermissionByRoleId($roleId){        
        $this->db->select('sy_permissions.*');
        $this->db->from('sy_permissions');
        $this->db->join('sy_role_permissions', 'sy_role_permissions.permission_id = sy_permissions.permission_id');
        $this->db->join('sy_roles', 'sy_roles.role_id = sy_role_permissions.role_id');
        $this->db->where('sy_roles.role_id',$roleId);
        $this->db->order_by("sy_permissions.permission_id", "asc");
        $this->db->order_by("sy_permissions.name", "asc");
        $query = $this->db->get();
        
        if($query->num_rows() > 0) return $query->result();
        return false;   
    }
    function loadPermission(){
        $permission = $this->getAllPermission();
        $mapping = array();
        
        foreach($permission As $item){
            $split = explode(".", $item->name);
            
            if( array_key_exists($split[0], $mapping) ){
                $mapping[$split[0]][] = array('row'=>$split[1],'col'=>$split[2],'permission_id'=>$item->permission_id,'checked'=>false);
            }else{
                $mapping[$split[0]][] = array('row'=>$split[1],'col'=>$split[2],'permission_id'=>$item->permission_id,'checked'=>false);
            }
            
        }
        return $mapping;
    }
    function getAllPermission(){        
        $this->db->select('*');
        $this->db->from('sy_permissions');
        $this->db->order_by("permission_id", "asc");
        $this->db->order_by("name", "asc");
        $query = $this->db->get();
        
        if($query->num_rows() > 0) return $query->result();
        return false;   
    }
    function insertRolePermission($data){
        $this->db->insert('sy_role_permissions', $data);
        //return $this->db->insert_id();
    }
    function deleteRolePermission($roleId, $permissionId){
        $this->db->delete('sy_role_permissions', array('role_id'=>$roleId,'permission_id' => $permissionId));
    }
}